package cn.dark.auth.core;

import cn.dark.auth.core.spring.YamlPropertySourceFactory;
import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Objects;

/**
 * 系统配置
 *
 * @author dark
 * @date 2023-10-22
 */
@Slf4j
@Configuration
@PropertySource(value = "classpath:sysConfig.yml", factory = YamlPropertySourceFactory.class)
@ConfigurationProperties(prefix = "auth")
public class SysConfig {

    private static SysConfig INSTANCE;

    /**
     * 异常账号列表
     */
    private List<Object> abnormalAccounts;

    /**
     * 是否允许不同设备类型同时登录
     */
    private boolean concurrent;

    /**
     * 生成token最大重试次数，-1表示一直重试
     */
    private Integer tokenGenRetryMaxTime;

    /**
     * token和session的超时时间，默认1天，单位秒
     */
    private Integer timeout;

    /**
     * 同一个账号同时登录最大token数量
     */
    private Integer maxTokenSize;

    @PostConstruct
    public void init() {
        log.info("当前配置：{}", this);
        INSTANCE = this;
    }

    public static List<Object> getAbnormalaccounts() {
        if (CollUtil.isEmpty(INSTANCE.abnormalAccounts)) {
            return Lists.newArrayList();
        }
        return INSTANCE.abnormalAccounts;
    }

    public static boolean isConcurrent() {
        return INSTANCE.concurrent;
    }

    public static int getTokenGenRetryMaxTime() {
        if (Objects.isNull(INSTANCE.tokenGenRetryMaxTime)) {
            return -1;
        }

        return INSTANCE.tokenGenRetryMaxTime;
    }

    public static int getTimeOut() {
        if (Objects.isNull(INSTANCE.timeout)) {
            return 24 * 60 * 60;
        }

        return INSTANCE.timeout;
    }

    public static int getMaxTokenSize() {
        if (Objects.isNull(INSTANCE.maxTokenSize)) {
            return 1;
        }

        return INSTANCE.maxTokenSize;
    }

    public void setAbnormalAccounts(List<Object> abnormalAccounts) {
        this.abnormalAccounts = abnormalAccounts;
    }

    public void setConcurrent(boolean concurrent) {
        this.concurrent = concurrent;
    }

    public void setTokenGenRetryMaxTime(Integer tokenGenRetryMaxTime) {
        this.tokenGenRetryMaxTime = tokenGenRetryMaxTime;
    }

    public void setTimeout(Integer timeout) {
        this.timeout = timeout;
    }

    public void setMaxTokenSize(Integer maxTokenSize) {
        this.maxTokenSize = maxTokenSize;
    }

    @Override
    public String toString() {
        return "SysConfig{" +
                "abnormalAccounts=" + abnormalAccounts +
                ", concurrent=" + concurrent +
                ", tokenGenRetryMaxTime=" + tokenGenRetryMaxTime +
                ", timeout=" + timeout +
                ", maxTokenSize=" + maxTokenSize +
                '}';
    }
}
